#!/bin/sh

set -e

if [ ! -d src ]; then
    echo "Not above src/"
    exit -1
fi

cwd=$(pwd)

version=$(r -e'cat(as.character(read.dcf("DESCRIPTION")[,"Version"]))')
echo "Working on version $version"


if [ -x /usr/bin/doxygen ]; then
    cd inst/doc
    rm -rf html/ latex/ man/
    cd ${cwd}
    cd src && ln -s ../inst/examples . && cd -

    ## see FAQ 17 for doxygen
    ( cat doxyfile ; echo PROJECT_NAME="\"MQL2R Version ${version}\"" ) | doxygen -

    rm src/examples
    cd ${cwd}
    pwd

    cd inst/doc
    zip -9r mql2r-doc-html.zip html/
    zip -9r mql2r-doc-man.zip man
    zip -9r mql2r-doc-latex.zip latex
    if [ -d ~/www/code/mql2r/ ]; then
	mv -v mql2r-doc-*.zip ~/www/code/mql2r/
	rsync --delete -avu html ~/www/code/mql2r/
    fi
    cd ${cwd}

    cd inst/doc/latex
    pdflatex refman
    pdflatex refman
    if [ -d ~/www/code/mql2r/ ]; then
	cp -vax refman.pdf ~/www/code/mql2r/RInside_refman.pdf
    fi
    cd ${cwd}

#    if [ -d ~/www/code/mql2r/ ]; then
#	cp -vax inst/doc/MQL2R-*.pdf ~/www/code/mql2r
#    fi
#    cd ${cwd}

fi